# proevolytx@gmail.com
# 2012/9/30

class StringStream:
    def __init__(s, string):
        s.string = string
        s.index = 0
        s.line = 1
        
    def Peek(s, length = 1):
        if s.index < len(s.string):
            return s.string[s.index : s.index + length]
        else:
            return None

    def PeekAt(s, length = 0):
        if s.index + length < len(s.string):
            return s.string[s.index + length]
        else:
            return None
        
    def Next(s, length = 1):
        for i in range(length):
            if s.Peek() == "\n":
                s.line += 1
            if s.index < len(s.string):
                s.index += 1

    def GetLineNum(s):
        return s.line

class FileStream(StringStream):
    def __init__(s, path):
        try:
            f = open(path, "rb")
            lines = "".join(f.readlines())
            f.close()
        except Exception, e:
            raise Exception("Unable to open file: " + path)
        StringStream.__init__(s, lines)
        
